package com.itheima.service.impl;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class UserServiceImpl implements UserService {
    private List<User> list = null;
    private User user = null;
    private SqlSession session = null;
    public List<User> findAll(){
        //获取SqlSession
        try {
            session = MapperUtil.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
             list = mapper.findAll();
            MapperUtil.close(session);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取代理对象

        return list;
    }
    public User findById(int id){
        //获取SqlSession
        try {
            session = MapperUtil.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
            user = mapper.findById(id);
            MapperUtil.close(session);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取代理对象
        return user;
    }

    public User findByNameAndPwd(String username, String pwd) {
        try {
            session = MapperUtil.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
            user = mapper.findByNameAndPwd(username,pwd);
            MapperUtil.close(session);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return user;
    }

    public List<User> findCondition(User user) {
        try {
            session = MapperUtil.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
            list = mapper.findCondition(user);
            MapperUtil.close(session);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<User> findByIds(List<Integer> lists) {
        try {
            session = MapperUtil.getSession();
            UserMapper mapper = session.getMapper(UserMapper.class);
            list = mapper.findByIds(lists);
            MapperUtil.close(session);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
